<style lang="less">
    @import './register.less';
</style>

<template>
    <div class="register" @keydown.enter="handleSubmit">
        <div class="register-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎注册
                </p>
                <div class="form-con">
                    <Form ref="registerForm" :model="registerForm" :rules="rules">
                        <FormItem label="姓名：" prop="fullName"> 
                            <Input v-model="registerForm.fullName" placeholder="请输入姓名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem label="账号：" prop="userName">
                            <Input v-model="registerForm.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>

                        <FormItem label="密码：" prop="password">
                            <Input type="password" v-model="registerForm.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem label="确认密码：" prop="rpassword">
                            <Input type="password" v-model="registerForm.rpassword" placeholder="请再次输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        
                        <FormItem label="性别：" prop="gender">
                            <RadioGroup v-model="registerForm.gender">
                                <Radio label="male">
                                    <span>男</span>
                                </Radio>
                                <Radio label="fomale">
                                    <span>女</span>
                                </Radio>
                                <Radio label="other">
                                    <span>保密</span>
                                </Radio>                                
                            </RadioGroup>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>注册</Button>
                        </FormItem>
                    </Form>                    
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
export default {
    data () {
        var $this = this;
        const valideRePassword = (rule, value, callback) => {
            if (value !== $this.registerForm.password) {
                callback(new Error('两次输入密码不一致'));
            } else {
                callback();
            }
        };

        return {
            registerForm: {
                userName: '',
                password: '',
                fullName: '',
                gender: 'other'
            },
            rules: {
                userName: [
                    { required: true, message: '请输入账号', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                rpassword: [
                    { required: true, message: '请再次输入密码', trigger: 'blur' },
                    { validator: valideRePassword, trigger: 'blur' }
                ],
                fullName: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                gender: [
                    { required: true, message: '请选择性别', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.registerForm.validate((valid) => {
                if (valid) {
                    console.log(this.registerForm.userName, this.registerForm.fullName);
                    
                    Cookies.set('user', this.registerForm.userName);
                    Cookies.set('password', this.registerForm.password);
                    
                    Cookies.set('access', 0);
                    
                    this.$router.push({
                        name: 'home_index'
                    });
                }
            });
        }
    }
};
</script>

<style>

</style>
